package com.ruoyi.wxlogin.controller;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.wxcommon.common.Result;
import com.ruoyi.wxlogin.service.IWeixinService;
import com.ruoyi.wxlogin.weixin.WXAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author crush
 * @since 2021-09-14
 */
@Slf4j
@RestController
@RequestMapping("/wx/get")
public class WeixinController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    IWeixinService weixinService;
    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.secret}")
    private String secret;
    @Autowired
    private WebClient webClient;
    //使用RestTemplate
    //获取手机号
    /*@PostMapping("/user/phone")
    public Result getPhone(@RequestParam(value = "code", required = false) String code) {
        // 获取token
        String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, secret);
        JSONObject token = JSON.parseObject(HttpUtil.get(token_url));

        // 使用前端code获取手机号码 参数为json格式
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token.getString("access_token");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", code);

        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
        System.out.println(httpEntity);
        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);
        //return R.ok().message("获取手机号码成功.").data(response.getBody());
        return Result.SUCCESS(response.getBody());
    }*/
    //使用WebClient
    //获取手机号
    @PostMapping("/user/phone")
    public Result getPhone(@RequestParam(value = "code", required = false) String code) {
        // 获取token
        String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, secret);
        JSONObject token = JSON.parseObject(HttpUtil.get(token_url));
        // 使用前端code获取手机号码 参数为json格式
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token.getString("access_token");
        // 构建请求体
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", code);
        // 使用 WebClient 发起 POST 请求
        Mono<Object> response = webClient.post()
                .uri(url)
                .body(BodyInserters.fromObject(paramMap))
                .retrieve()
                .bodyToMono(Object.class);
        // 可以在这里对响应进行处理
        return Result.SUCCESS(response.block()); // 返回结果
    }
    //这个就是那个使用传code进来的接口
    @GetMapping("/sessionId/code")
    public Result getSessionId(String code){
        return weixinService.getSessionId(code) ;
    }
    @GetMapping("/user/userInfo")
    public Result userInfo(String openId){
        return weixinService.userInfo(openId);
    }
    @PostMapping("/authLogin")
    public Result authLogin(WXAuth wxAuth) {
        Result result = weixinService.authLogin(wxAuth);
        log.info("{}",result);
        return result;
    }
}
